<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div class="layui-row">
        <form class="layui-form" lay-filter="myform" id="myform">
          <input type="reset" id="btReset" class="layui-hide" >
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">分类名称</label>
              <div class="layui-input-inline">
                <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <div class="layui-btn-group">
                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
              </div>
            </div>
          </div>
        </form>
      </div>
      <div class="row">
        <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
      </div>
    </div>
  </div>
</div>

<script th:inline="none"></script>
<script>
  layui.use(['table','form','element'], function(){
    var table = layui.table,
            element = layui.element,
            form = layui.form;
    table.render({
      elem: '#main-table'
      ,height: (top.height-85)+'px'
      ,url: 'symbol/getPageList'
      ,title: '标签组列表'
      ,page: true
      ,toolbar: '#toolbar'
      ,cols: [
        [
          {type: 'checkbox'}
          ,{field: 'code', title: "字符",width:280,templet:function(d){
            return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\")'>" + d.code + "</a>";
          }}
          ,{field: 'value',title: "转义",width:120,edit:'text'}
        ]
      ]
      ,done:function(){
        enableRowClick(table,form,'main-table',"main-table");
      }
    });
    table.on('edit(main-table)', function(obj){
      var value = obj.value //得到修改后的值
              ,data = obj.data //得到所在行所有键值
              ,field = obj.field; //得到字段
      axios.post('libCategory/save',obj.data).then(
              res=>{
                layer.msg('保存成功',{icon:1});
                table.reload('main-table');
              }
      )
    });
    //头工具栏事件
    table.on('toolbar(main-table)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'add':
          layer.open({
            id:"openwin",
            type: 2,
            title:'增加题库分类',
            area:['1100px','600px'],
            content:  'rooter?url=base/category/add-category',
            btn:['保存','关闭'],
            yes:function(index,layero){
              var dataForm = layer.getChildFrame('form', index);
              dataForm.find("#submitBtn").click();
              return false
            }
          });
          break;
        case 'delete':
          var data = checkStatus.data;
          if(data.length>0){
            var values = {};
            var ids=[];
            $.each(data,function(i,v){
              ids.push(v.id);
            })
            values['ids'] = ids.join(",");
            layer.confirm('确定要删除当前行吗?', {icon: 3, title:'提示'}, function(index){
              axios.post("libCategory/delete",values).then(res=>{
                if (res.data.code == 1) {
                  layer.msg(res.msg);
                } else {
                  layer.close(index);
                  table.reload('main-table');
                  layer.msg("删除成功。");
                }
              })
            });
          }else{
            layer.msg("请选择操作数据!")
          }
          break;
        case 'edit':
          var data2 = checkStatus.data;
          if(data2.length>0){
            edit(data2[0].id);
          }else{
            layer.msg("请选择操作数据！")
          }
          break;
      };
    });
    var $ = layui.$, active = {
      query: function(){

        table.reload("main-table",{where:{name:$("#name").val()}})
      },
      resetBtn:function () {
        $("#btReset").click();

      }
    };
    window.loadGrid = function(tableName){
      table.reload(tableName);
    }
    window.queryQuestion=function (id,name){
      localStorage.setItem("libid",id);
      top.$("#29").click();
    }
    $('.layui-form .layui-btn').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
  function edit(id){
    layer.open({
      id:"openwin",
      type: 2,
      title:'修改标签组',
      area:['800px','600px'],
      content:  'rooter?url=base/category/add-category&id='+id,
      btn:['保存','关闭'],
      yes:function(index,layero){
        var dataForm = layer.getChildFrame('form', index);
        dataForm.find("#submitBtn").click();
        return false
      }
    });
  }

</script>
<script type="text/html" id="toolbar">
  <div class="layui-btn-group">
    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
    <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
  </div>
</script>
</body>
</html>